const app = getApp();
const {
  htmlEncode,
  checkInputCode
} = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    towerUrl: app.globalData.towerUrl,
    tableArray: [{
        table: 'CS1',
        maxSpeed: 500
      },
      {
        table: 'CS2',
        maxSpeed: 420
      },
      {
        table: 'CS3',
        maxSpeed: 500
      },
      {
        table: 'CS4',
        maxSpeed: 500
      },
      {
        table: 'FS1',
        maxSpeed: 320
      },
      {
        table: 'FS2',
        maxSpeed: 320
      },
      {
        table: 'RW1',
        maxSpeed: 2000
      }
    ],
    tableIndex: '',
    upNoArray: ['第1退纸架', '第2退纸架', '第3退纸架', '第4退纸架', '第5退纸架', '第6退纸架', '第7退纸架', '第8退纸架'],
    upNoIndex: '',
    qcArray: [],
    qcIndex: '',
    coreArray: ['塑料', '纸质', '拼接管'],
    coreIndex: '',
    checkTypeArray: ['纸卷不圆', '软边', '线长不一', '接头不良', '原纸打折/死纹', '标签与实物不符', '断面不齐', '原纸夹有烂纸', '打底不良', '隆条/横活纹', '撞伤'],
    checkTypeIndex: '',
    severityArray: ['轻微', '严重'],
    severityIndex: '',
    paperId: '',
    captain: '',
    mesData: {},
    speedRate: '',
    speed: '',
    maxSpeed: ''
  },

  tableChange(e) {
    console.log('e', e);
    this.setData({
      tableIndex: e.detail.value,
      maxSpeed: this.data.tableArray[e.detail.value].maxSpeed,
      speed: '',
      speedRate: ''
    })
  },
  tableCancel() {
    this.setData({
      tableIndex: '',
      maxSpeed: '',
      speed: '',
      speedRate: ''
    })
  },

  qcChange(e) {
    this.setData({
      qcIndex: e.detail.value
    })
  },
  qcCancel() {
    this.setData({
      qcIndex: ''
    })
  },

  coreChange(e) {
    this.setData({
      coreIndex: e.detail.value
    })
  },
  coreCancel() {
    this.setData({
      coreIndex: ''
    })
  },

  checkTypeChange(e) {
    this.setData({
      checkTypeIndex: e.detail.value
    })
  },
  checkTypeCancel() {
    this.setData({
      checkTypeIndex: ''
    })
  },

  severityChange(e) {
    this.setData({
      severityIndex: e.detail.value
    })
  },
  severityCancel() {
    this.setData({
      severityIndex: ''
    })
  },

  upNoChange(e) {
    this.setData({
      upNoIndex: e.detail.value
    })
  },
  upNoCancel() {
    this.setData({
      upNoIndex: ''
    })
  },

  speedChange(e) {
    var speedTmp = e.detail.value;
    console.log('speed', speedTmp);
    if (!this.data.maxSpeed) {
      wx.showModal({
        title: '提示',
        content: '请先选定机台',
        showCancel: false
      })
      this.setData({
        speed: ''
      })
      return;
    }
    if (Number(speedTmp) > Number(this.data.maxSpeed)) {
      wx.showModal({
        title: '提示',
        content: '输入测速不能大于最大测速',
        showCancel: false
      })
      this.setData({
        speed: ''
      })
      return;
    }
    this.setData({
      // speedRate: (speedTmp/this.data.maxSpeed).toFixed(2)*100 + '%'
      speedRate: Math.round((speedTmp / this.data.maxSpeed) * 100) + '%'
    })
  },

  getData() {
    var that = this;
    wx.request({
      url: that.data.towerUrl + '/scrollCheck/wx/getDataByUUID?uuid=' + that.data.paperId,
      method: 'GET',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取mes信息:', res.data);
        var data = res.data;
        if (data.code == 200) {
          if(data.data){
            that.setData({
              mesData: data.data
            })
          }else{
            wx.showModal({
              title: '提示',
              content: '查无纸卷数据',
              showCancel:false,
              success(res) {
                if (res.confirm) {
                  wx.navigateBack();
                }
              }
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: '查询纸卷数据有误',
            showCancel:false,
            success(res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          })
        }
      }
    });
  },

  getQcList() {
    var that = this;
    wx.request({
      url: that.data.towerUrl + '/api/tower/moa/wx/getEnumsByKey?key=check_qc',
      method: 'POST',
      header: {
        'Content-Type': 'application/json' // 请求头  
      },
      success(res) {
        console.log('获取qc信息:', res.data);
        that.setData({
          qcArray: res.data.data
        })
      }
    });
  },

  submitForm(e) {
    wx.showLoading({
      title: '提交中',
      mask: true
    })
    var that = this;
    const formData = e.detail.value;
    formData.paperId = that.data.mesData.paperId;
    formData.rewindDate = that.data.mesData.rewindDate;
    formData.rewindMachine = that.data.mesData.rewindMachine;
    formData.rewindCount = that.data.mesData.rewindCount;
    formData.paperType = that.data.mesData.paperType;
    formData.width = that.data.mesData.width;
    formData.position = that.data.mesData.position;
    formData.productDate = that.data.mesData.productDate;
    formData.xpos = that.data.mesData.xpos;
    formData.jumboRollId = that.data.mesData.jumboRollId;
    formData.speedRate = that.data.speedRate;
    formData.backStandNo = that.data.mesData.backStandNo;
    formData.captain = that.data.captain;
    var errMsg = "";
    if (!formData.cutTable) {
      errMsg = errMsg + "机台不能为空;";
    }
    if (!formData.qc) {
      errMsg = errMsg + "QC确认人不能为空;";
    }
    if (!formData.paperCore) {
      errMsg = errMsg + "纸芯管不能为空;";
    }
    if (!formData.checkCategory) {
      errMsg = errMsg + "纸病分类不能为空;";
    }
    if (!formData.severity) {
      errMsg = errMsg + "严重程度不能为空;";
    }
    if (!formData.upPaperNo) {
      errMsg = errMsg + "机台上纸架不能为空;";
    }
    if (!formData.speed) {
      errMsg = errMsg + "裁纸速度不能为空;";
    }
    console.log('form发生了submit事件，携带数据为：', formData);
    if (errMsg.length > 0) {
      wx.hideLoading()
      wx.showModal({
        title: '提示',
        content: errMsg,
        showCancel: false
      })
      return;
    }

    // 向服务器提交表单数据
    wx.request({
      url: that.data.towerUrl + '/scrollCheck/wx/add',
      method: 'POST',
      header: {},
      data: formData,
      success: function (res) {
        wx.hideLoading();
        console.log('表单提交成功', res);
        if (200 == res.data.code) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/menu3/menu3'
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      },
      fail: function (err) {
        wx.hideLoading()
        console.error('表单提交失败', err);
        wx.showToast({
          title: '表单提交失败',
          icon: 'none'
        });
      }
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      paperId: options.paperId,
      captain: wx.getStorageSync('userinfo').siocCnName
    })
    this.getData();
    this.getQcList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})